package com.zycfc.zsf.boot.util.number;

import com.google.common.primitives.*;
import org.apache.commons.lang3.math.*;
import org.apache.commons.lang3.*;
import java.util.*;

public abstract class NumberMoreUtils
{
    public static byte[] toBytes(final int value) {
        return Ints.toByteArray(value);
    }
    
    public static byte[] toBytes(final long value) {
        return Longs.toByteArray(value);
    }
    
    public static byte[] toBytes(final double val) {
        return toBytes(Double.doubleToRawLongBits(val));
    }
    
    public static int toInt(final byte[] bytes) {
        return Ints.fromByteArray(bytes);
    }
    
    public static long toLong(final byte[] bytes) {
        return Longs.fromByteArray(bytes);
    }
    
    public static double toDouble(final byte[] bytes) {
        return Double.longBitsToDouble(toLong(bytes));
    }
    
    public static boolean isNumber(final String str) {
        return NumberUtils.isNumber(str);
    }
    
    public static boolean isHexNumber(final String value) {
        final int index = value.startsWith("-") ? 1 : 0;
        return value.startsWith("0x", index) || value.startsWith("0X", index) || value.startsWith("#", index);
    }
    
    public static int toInt(final String str) {
        return NumberUtils.toInt(str, 0);
    }
    
    public static int toInt(final String str, final int defaultValue) {
        return NumberUtils.toInt(str, defaultValue);
    }
    
    public static long toLong(final String str) {
        return NumberUtils.toLong(str, 0L);
    }
    
    public static long toLong(final String str, final long defaultValue) {
        return NumberUtils.toLong(str, defaultValue);
    }
    
    public static double toDouble(final String str) {
        return NumberUtils.toDouble(str, 0.0);
    }
    
    public static double toDouble(final String str, final double defaultValue) {
        return NumberUtils.toDouble(str, defaultValue);
    }
    
    public static Integer toIntObject(final String str) {
        return toIntObject(str, null);
    }
    
    public static Integer toIntObject(final String str, final Integer defaultValue) {
        if (StringUtils.isEmpty((CharSequence)str)) {
            return defaultValue;
        }
        try {
            return Integer.valueOf(str);
        }
        catch (NumberFormatException nfe) {
            return defaultValue;
        }
    }
    
    public static Long toLongObject(final String str) {
        return toLongObject(str, null);
    }
    
    public static Long toLongObject(final String str, final Long defaultValue) {
        if (StringUtils.isEmpty((CharSequence)str)) {
            return defaultValue;
        }
        try {
            return Long.valueOf(str);
        }
        catch (NumberFormatException nfe) {
            return defaultValue;
        }
    }
    
    public static Double toDoubleObject(final String str) {
        return toDoubleObject(str, null);
    }
    
    public static Double toDoubleObject(final String str, final Double defaultValue) {
        if (StringUtils.isEmpty((CharSequence)str)) {
            return defaultValue;
        }
        try {
            return Double.valueOf(str);
        }
        catch (NumberFormatException nfe) {
            return defaultValue;
        }
    }
    
    public static Integer hexToIntObject(final String str) {
        return hexToIntObject(str, null);
    }
    
    public static Integer hexToIntObject(final String str, final Integer defaultValue) {
        if (StringUtils.isEmpty((CharSequence)str)) {
            return defaultValue;
        }
        try {
            return Integer.decode(str);
        }
        catch (NumberFormatException nfe) {
            return defaultValue;
        }
    }
    
    public static Long hexToLongObject(final String str) {
        return hexToLongObject(str, null);
    }
    
    public static Long hexToLongObject(final String str, final Long defaultValue) {
        if (StringUtils.isEmpty((CharSequence)str)) {
            return defaultValue;
        }
        try {
            return Long.decode(str);
        }
        catch (NumberFormatException nfe) {
            return defaultValue;
        }
    }
    
    public static String toString(final int i) {
        return Integer.toString(i);
    }
    
    public static String toString(final Integer i) {
        return i.toString();
    }
    
    public static String toString(final long l) {
        return Long.toString(l);
    }
    
    public static String toString(final Long l) {
        return l.toString();
    }
    
    public static String toString(final double d) {
        return Double.toString(d);
    }
    
    public static String toString(final Double d) {
        return d.toString();
    }
    
    public static String to2DigitString(final double d) {
        return String.format(Locale.ROOT, "%.2f", d);
    }
    
    public static int toInt32(final long x) throws IllegalArgumentException {
        if ((int)x == x) {
            return (int)x;
        }
        throw new IllegalArgumentException("Int " + x + " out of range");
    }
}
